<template>
  <div class="dashboard-container">
    <div class="app-container">
      <h2>
        <!-- 组织架构内容 -->
        <el-card class="tree-card">
          <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
          <!-- 放置一个el-tree -->
          <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
            <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
            <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
            <tree-tools slot-scope="{data}" :tree-node="data" @addDepts="addDepts" @editDepts="editDepts" @delDepts="getDepartments" />
          </el-tree>
        </el-card>
      </h2>
    </div>
    <!-- 放置新增弹层组件 -->
    <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      company: {},
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false, // 默认不显示
      node: null // 记录当前点击的node节点
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(res.depts, '') // 需要转化成数据结构
      console.log(res)
    },
    // 监听tree-tools中触发的点击子部门的事件
    // node是我们点击的部门
    addDepts(node) {
      this.showDialog = true// 显示弹层
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node
      // 应该在这里调取获取部门详情的方法
      console.log(this.$refs.addDept)
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
  .tree-card{
    padding: 30px  140px;
    font-size: 14px;
    font-weight: normal;
  }
</style>
